package hr.tel.fer.projekt;

import android.app.Activity;
import android.os.Bundle;

import java.net.URL;
 
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
 
import org.anddev.android.weatherforecast.weather.GoogleWeatherHandler;
import org.anddev.android.weatherforecast.weather.WeatherCurrentCondition;
import org.anddev.android.weatherforecast.weather.WeatherForecastCondition;
import org.anddev.android.weatherforecast.weather.WeatherSet;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
 
import android.util.Log;
import android.widget.TextView;
 
public class Main extends Activity {
       
        private final String MY_DEBUG_TAG = "WeatherForcaster";
 
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle icicle) {
                super.onCreate(icicle);
                
                setContentView(R.layout.main);
                
                TextView today = (TextView) findViewById(R.id.today);
                TextView tomorrow = (TextView) findViewById(R.id.tomorrow);
                
                try {
                        /* Create a URL we want to load some xml-data from. */
                        URL url = new URL("http://www.google.com/ig/api?weather=,,,50500000,30500000");
 
                        /* Get a SAXParser from the SAXPArserFactory. */
                        SAXParserFactory spf = SAXParserFactory.newInstance();
                        SAXParser sp = spf.newSAXParser();
 
                        /* Get the XMLReader of the SAXParser we created. */
                        XMLReader xr = sp.getXMLReader();
                        /* Create a new ContentHandler and apply it to the XML-Reader*/
                        GoogleWeatherHandler myExampleHandler = new GoogleWeatherHandler();
                        xr.setContentHandler(myExampleHandler);
                       
                        /* Parse the xml-data from our URL. */
                        xr.parse(new InputSource(url.openStream()));
                        /* Parsing has finished. */
 
                        WeatherSet weatherSet = myExampleHandler.getWeatherSet();
                        WeatherCurrentCondition current = weatherSet.getWeatherCurrentCondition();
                        WeatherForecastCondition forecast = weatherSet.getWeatherForecastCondition(WeatherSet.TODAY_PLUS_1);
                       
                        today.setText(current.toString());
                        tomorrow.setText(forecast.toString());
                        
                } catch (Exception e) {
                        /* Display any Error to the GUI. */
                        today.setText("Error: " + e.getMessage());
                        Log.e(MY_DEBUG_TAG, "WeatherQueryError", e);
                }
        }
}